home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / graphics / mandlbx1.arc / DRAW.C next >
C/C++ Source or Header  |  1985-11-20  |  7KB  |  310 lines

  1. /* draw.c    draws holigraphically on crt
  2.     dr_iRastWindow()  initializes raster coords of window
  3.     dr_iProgCoords()  initializes user floating point coords of screen
  4.  
  5.     dr_zoom()    zoom in to subsection of screen
  6.     dr_Vreset()    resets V stack
  7.     dr_Vpush()    push V onto stack
  8.     dr_Vpop()    pop V from stack
  9.  
  10.     dr_sBox()    starts the filling in of the above box
  11.     dr_compute()    divides sub box into required rectangles
  12.     dr_area    generate a box of points around the current one
  13.  
  14.     dr_xy()    computes a mandelbrot value and puts in buffer
  15.     dr_putBuf()    puts buffer on crt
  16.     dr_clip        sets clip rectangle for dr_putBuf
  17.  
  18.     load_clut()
  19.  */
  20.  
  21. #include <obdefs.h>
  22. #include <define.h>
  23. #include <gemdefs.h>
  24. #include <osbind.h>
  25. #include <stdio.h>
  26. #include <debug.h>
  27.  
  28. #define L (long)
  29. #define LOG_CELLS 9
  30.                 /* was 9 */
  31. #define ASPECT_F (16.0/19.0)    /* X dimension of pixel / Y~ */
  32. int vs_handle;            /* virtual workstation handle */
  33. int wi_handle;            /* window handle */
  34.  
  35. /**** shared with window.c: */
  36. double Vx0, Vy0, Vxw, Vyw;    /* Size of viewed area, in real numbers */
  37. int Niter = 256;        /* max number of iterations */
  38. int box_invisible;        /* if 0, boxes get outlined in black */
  39.  
  40. #define Nunzoom 50        /* stack of zoom-ins, to allow unzoom */
  41. int Iunzoom;
  42. struct unzoom
  43. {    double x0, y0,  xw, yw;
  44. } unzoom[Nunzoom];
  45.  
  46. long Tx0, Ty0;            /* Transform from Raster Coords to Prog Coords*/
  47. double Txx, Tyy;
  48. /* was pc X Y min gain */
  49.  
  50. int Wx0, Wy0;        /* Display Window Position, in Raster Coords */ 
  51. int Wxw, Wyw;
  52. int Wx1, Wy1;
  53.  
  54. int Cx0, Cx1, Cy0, Cy1; /* clip window */
  55.  
  56. int len;            /* side in pixels of boxes being generated */
  57. long count;
  58. int count_slider, slider;    /* of bar graph */
  59.  
  60. #define SIZEOF_BUF 160
  61. int bufI, *bufP;
  62. int buf[SIZEOF_BUF+1][5];
  63.  
  64. char str[128];
  65. int v_pr_ct = 3;
  66. #define v_printf(a,b,c) sprintf(str,a,b,c);v_gtext(handl,30,10*v_pr_ct++,str);
  67.  
  68.  
  69. dr_iRastWindow(X0,Y0, Xw,Yw)
  70. int X0, Y0, Xw, Yw;                /* of window, in rc's */
  71. {    Wx0 = X0;    Wy0 = Y0;
  72.     Wxw = Xw;    Wyw = Yw;
  73.     Wx1 = Wx0 + Wxw -1;
  74.     Wy1 = Wy0 + Wyw -1;
  75. }
  76. long debug1;
  77.  
  78. dr_iProgCoords(x0,y0, xw,yw)
  79. double x0, y0, xw, yw;
  80. {    double ratio, dif;
  81.     long magnification;
  82.  
  83.     ratio = (double)Wxw * ASPECT_F/(double)Wyw;
  84.     if (xw < (dif= ratio * yw))
  85.     {    x0 -= (dif - xw)/2;        /* expand pcX */
  86.         xw = dif;
  87.     }else
  88.     {    dif = xw / ratio;            /* expand pcY */
  89.         y0 -= (dif - yw)/2;
  90.         yw = dif;
  91.     }
  92.     Vx0 = x0;
  93.     Vy0 = y0;
  94.     Vxw = xw;
  95.     Vyw = yw;
  96.  
  97.     Tx0 = x0 * (double)0x4000000; Ty0 = y0 * (double)0x4000000;
  98.     Tyy = yw/(double)Wyw * (double)0x4000000; /*2^26*/;
  99.     Txx = Tyy * ASPECT_F;
  100.  
  101.     magnification = 1.0/xw + 1.0;
  102.     clip_full();
  103.     sprintf(str, "%ldx  ", magnification);
  104.     v_gtext(vs_handle, Wx0+20, Wy0-3, str);
  105.     clip_current();
  106.     
  107. }
  108.  
  109. dr_zoom(x0,y0, x1,y1)
  110. int x0,y0, x1,y1;
  111. {    double Rx, Ry;
  112.     int dx, dy, i;
  113.     if (x0 > x1)
  114.     {    i=x0; x0=x1; x1=i;
  115.     }
  116.     if (y0 > y1)
  117.     {    i=y0; y0=y1; y1=i;
  118.     }
  119.     dr_Vpush();
  120.     Vx0 += (double)(x0-Wx0) / (double)Wxw * Vxw;
  121.     Vy0 += (double)(y0-Wy0) / (double)Wyw * Vyw;
  122.     Vxw *= Rx = (double)(x1-x0+1) / (double)Wxw;
  123.     Vyw *= Ry = (double)(y1-y0+1) / (double)Wyw;
  124.  
  125.     dr_iProgCoords(Vx0, Vy0,  Vxw, Vyw);
  126. }
  127.  
  128. dr_Vpush()
  129. {    if (Iunzoom == Nunzoom) 
  130.     {    form_alert(0,"[1][Unzoom stack overflow,|UNZOOM is now broken.]\
  131.             [I understand]");
  132.         Iunzoom++;
  133.     }
  134.     else if (Iunzoom < Nunzoom)
  135.     {    unzoom[Iunzoom].x0 = Vx0;
  136.         unzoom[Iunzoom].y0 = Vy0;
  137.         unzoom[Iunzoom].xw = Vxw;
  138.         unzoom[Iunzoom++].yw = Vyw;
  139. }    }
  140.  
  141. dr_Vpop()
  142. {    if (Iunzoom<Nunzoom && Iunzoom-- >0 )
  143.     {    Vx0 = unzoom[Iunzoom].x0;
  144.         Vy0 = unzoom[Iunzoom].y0;
  145.         Vxw = unzoom[Iunzoom].xw;
  146.         Vyw = unzoom[Iunzoom].yw;
  147.         dr_iProgCoords(Vx0, Vy0,  Vxw, Vyw);
  148.         return(1);
  149.     }    
  150.     else
  151.     {    form_alert(0,"[1][No previous place to|zoom back to.|\
  152. Command ignored.][acknowlege]");
  153.         Iunzoom = 0;
  154.         return(0);
  155. }    }
  156.  
  157. dr_Vreset()
  158. {    Iunzoom = 0;
  159. }
  160.  
  161. dr_Vprint()
  162. {    int i;
  163.     printf("V = (%f,%f) + (%f,%f)\n",Vx0,Vy0,Vxw,Vyw);
  164.     for (i = Iunzoom; --i>=0;)
  165.         printf("%d = (%f,%f) + (%f,%f)\n",i, unzoom[i].x0,unzoom[i].y0,
  166.             unzoom[i].xw,unzoom[i].yw);
  167.     wait();
  168. }
  169.  
  170. wait()
  171. {    long i;
  172.     for (i=0; i<2000000; i++);
  173. }
  174.  
  175. dr_sBox()    /* called when finally going to restart */
  176. {    int pxyarray[4];
  177.  
  178.     st_inz(LOG_CELLS, Wxw, Wyw);
  179.     count_slider = 0; slider = 0;        /* of bar graph */
  180.     wind_set(wi_handle, WF_VSLIDE, 0, 0, 0, 0);
  181.  
  182.     vsf_interior(vs_handle,2);    vsf_style(vs_handle,8);    
  183. }
  184.  
  185. dr_clip(x0,y0, xw,yw)
  186. int x0,y0, xw,yw;
  187. {    Cx0 = x0;
  188.     Cy0 = y0;
  189.     Cx1 = x0 + xw - 1;
  190.     Cy1 = y0 + yw - 1;
  191. }
  192.  
  193. dr_compute()
  194. {    dprintf(stderr, "dr_compute()\n");/**/
  195.     bufP = buf;
  196.     bufI = 0;        /* (64 --- space for recursive splits) */
  197.     while (bufI < SIZEOF_BUFF - 64) /* or LOG_CELLS*3 -5 */
  198.     {    dprintf(stderr, "dr_compute: bufI=%d\n",bufI);/**/
  199.         if (st_do_next() == 0) 
  200.         {    dprintf(stderr, "st_do_next returned 0\n");/**/
  201.             break;
  202.     }    }
  203.     dprintf(stderr, "dr_compute returning %d\n", bufI);/**/
  204.     return (bufI);
  205. }
  206.  
  207. dr_area(x0, y0, len)
  208. int x0, y0, len;
  209. {    int i, j;
  210.     bufP = buf;
  211.     bufI = 0;
  212.     for (i = -len; i<=len; i++)
  213.         for (j = -len; j<=len; i++)
  214.             dr_xy(x0+i, y0+j, 1);
  215. }
  216.  
  217. dr_xy(x0, y0, len)
  218. int x0, y0, len;
  219. {    long Zx, Zy;
  220.     long pcX, pcY;
  221.     int x,y;
  222.     int count, count_max, color;
  223.     char str[128];
  224.     
  225.     dprintf(stderr, "dr_xy: X=%x, Y=%x, l=%x, ", x0, y0, len);/**/
  226.     if (x0<Wxw && y0<Wyw)
  227.     {    pcX = (long)((double)x0 * Txx) + Tx0;
  228.         pcY = (long)((double)y0 * Tyy) + Ty0;
  229.         Zx=0; Zy=0;
  230.         count = Niter - mandels(L Niter, pcY, pcX, &Zy,&Zx);
  231. /*        count = (3*x0 + 2*y0)/57;/**/
  232.         if (count==Niter)
  233.         {    color = 1;
  234.             count = 32757;
  235.         }
  236.         else
  237.             color = ((count & 0x7fff)>>2)  %14 +2;
  238.         if (bufI > SIZEOF_BUF)
  239.             panic("write bufI overflow\n", 0L, 0L);
  240.         *bufP++ = color;
  241.         *bufP++ = ((x = Wx0 + x0) < Cx0)? Cx0: x;
  242.         *bufP++ = ((y = Wy0 + y0) < Cy0)? Cy0: y;
  243.         *bufP++ = ((x += len-1) > Cx1)? Cx1: x;
  244.         *bufP++ = ((y += len-1) > Cy1)? Cy1: y;
  245.         bufI++;
  246.         dprintf(stderr, "c=%d, bI=%d\n", count, bufI);/**/
  247.     }
  248.     else    
  249.     {    dprintf(stderr, "off-screen\n");/**/
  250.         count = -1;
  251.     }
  252.     return (count);
  253. }
  254.  
  255. dr_putBuf(box_count)
  256. int box_count;
  257. {    int i;
  258.     int *p;
  259.  
  260.     draw_on_screen();
  261.     st_check("dr_putBuf: \b\b\b\b\b\b\b\b\b\b\b");
  262.     for (i=0; i<box_count; i++)
  263.     {    p = buf[i];        
  264.         if (p[1]==p[3] || box_invisible)
  265.         {    vsf_color(vs_handle, *p);    /*draw colored box*/
  266.             v_bar(vs_handle, p+1);
  267.         }else
  268.         {    vsf_color(vs_handle, 0);    /*draw white box*/
  269.             v_bar(vs_handle, p+1);
  270.             vsf_color(vs_handle, *p);    /*draw colored box*/
  271.             p[1]++; p[2]++;            /*(smaller)*/
  272.             v_bar(vs_handle, p+1);
  273.         }
  274.         if (--count_slider <= 0)
  275.         {    wind_set(wi_handle, WF_VSLSIZE, 1000-slider, 0, 0, 0);
  276.             clip_full();
  277.             sprintf(str,"%d%% ", slider/10); 
  278.             v_gtext(vs_handle, Wx1-39, Wy0-3, str);
  279.             clip_current();
  280.             count_slider = (L Wxw*Wyw)/100;
  281.             slider += 10;
  282.     }    }
  283.     st_check("painted\b\b\b\b\b\b\b");
  284.     view_screen();
  285. }
  286.  
  287. rotate_clut(handle,clut)
  288. int handle, clut[16][3];
  289. {    int i, r,g,b;
  290.     r = clut[15][0];
  291.     g = clut[15][1];
  292.     b = clut[15][2];
  293.     for (i=15; i>2; i--)
  294.     {    clut[i][0] = clut[i-1][0];
  295.         clut[i][1] = clut[i-1][1];
  296.         clut[i][2] = clut[i-1][2];
  297.     }
  298.     clut[2][0] = r;
  299.     clut[2][1] = g;
  300.     clut[2][2] = b;
  301.     load_clut(handle,clut,(int *)0);
  302. }
  303. load_clut(handle, new, old)
  304. int handle, new[16][3], old[16][3];
  305. {    int i;
  306.     for (i=0; i<16; i++)
  307.     {    if (old) vq_color(handle, i, 0, old[i]);
  308.         vs_color(handle, i, new[i]);
  309. }    }
  310.